package com.ms.banner.view;


import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;
import ohos.app.Context;

public class ArcShapeView extends Component implements Component.DrawTask {

    private Paint mPaint;
    private Path mPath;
    private int arcHeight = 0;
    private Color startColor = Color.WHITE;
    private Color endColor = Color.WHITE;
    private int direction = 0;

    public ArcShapeView(Context context) {
        this(context, null);
    }

    public ArcShapeView(Context context, AttrSet attrs) {
        this(context, attrs, "");
    }

    public ArcShapeView(Context context, AttrSet attrs, String defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL_STYLE);
        mPath = new Path();
        addDrawTask(this);
    }

    public void setArcHeight(int arcHeight) {
        this.arcHeight = arcHeight;
    }

    public void setBackground(Color startColor, Color endColor) {
        this.startColor = startColor;
        this.endColor = endColor;
    }

    public void setDirection(int direction) {
        this.direction = direction;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        Point point1 = new Point(0, 0);
        Point point2 = new Point(getWidth(),getHeight());
        Point[] points = {point1, point2};
//            Color[] colors = {Color.RED, Color.WHITE};
        Color[] colors = {startColor, endColor};
        Shader shader = new LinearShader(points, new float[]{0.1f,0.7f}, colors, Shader.TileMode.CLAMP_TILEMODE);
        mPaint.setShader(shader,Paint.ShaderType.LINEAR_SHADER);
        if (direction == 0) {
            //up
            mPath.moveTo(0, getHeight());
            mPath.quadTo(getWidth() / (float)2, getHeight() - 2 * arcHeight, getWidth(), getHeight());
            canvas.drawPath(mPath, mPaint);
        } else {
            //down
            mPath.moveTo(0, getHeight() - arcHeight);
            mPath.lineTo(0, getHeight());
            mPath.lineTo(getWidth(), getHeight());
            mPath.lineTo(getWidth(), getHeight() - arcHeight);
            mPath.quadTo(getWidth() / (float)2, getHeight() + arcHeight, 0, getHeight() - arcHeight);
            canvas.drawPath(mPath, mPaint);
        }
    }
}
